package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.DictDataDTO;
import com.ktjy.dto.DictTypeDto;
import com.ktjy.pojo.DictData;
import com.ktjy.pojo.DictType;
import com.ktjy.service.IDictDataService;
import com.ktjy.service.IDictTypeService;
import com.ktjy.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-20
 */
@RestController
@RequestMapping("/api/sys/dictType")
@Tag(name = "字典类型模块")
public class DictTypeController {

    @Autowired
    private IDictTypeService dictTypeService;

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPerm('system:dict:list')")
    @Operation(summary = "字典列表接口")
    public AjaxResult list(DictTypeDto dictTypeDto,
                           @Parameter(description = "页码数") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                           @Parameter(description = "每页显示条数") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        IPage<DictType> page = dictTypeService.findPage(dictTypeDto, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('system:dict:add')")
    @Operation(summary = "添加字典接口")
    public AjaxResult add(@RequestBody DictTypeDto dictTypeDto) {
        DictType dictType = new DictType();
        BeanUtils.copyProperties(dictTypeDto, dictType);
        boolean save = dictTypeService.save(dictType);
        return save ? AjaxResult.success() : AjaxResult.error("添加失败");
    }

    @PutMapping("/update")
    @PreAuthorize("@ss.hasPerm('system:dict:edit')")
    @Operation(summary = "修改字典接口")
    public AjaxResult update(@RequestBody DictTypeDto dictTypeDto) {
        DictType dictType = new DictType();
        BeanUtils.copyProperties(dictTypeDto, dictType);
        boolean update = dictTypeService.updateById(dictType);
        return update ? AjaxResult.success() : AjaxResult.error("修改失败");
    }

    //根据id查询字典详情
    @GetMapping("/view/{id}")
    @PreAuthorize("@ss.hasPerm('system:dict:query')")
    @Operation(summary = "查询字典接口")
    public AjaxResult view(@PathVariable("id") Long id) {
        DictType dictType = dictTypeService.getById(id);
        return AjaxResult.success("查询成功", dictType);
    }


    @DeleteMapping("/del/{id}")
    @PreAuthorize("@ss.hasPerm('system:dict:remove')")
    @Operation(summary = "删除字典接口")
    public AjaxResult delete(@PathVariable("id") Long id) {
        boolean remove = dictTypeService.removeById(id);
        return remove ? AjaxResult.success() : AjaxResult.error("删除失败");
    }

    //查询全部字典类型
    @GetMapping("/listAll")
    @Operation(summary = "查询全部字典接口")
    @PreAuthorize("@ss.hasPerm('system:dict:query')")
    public AjaxResult listAll() {
        List<DictType> list = dictTypeService.list();
        return AjaxResult.success("查询成功", list);
    }

}
